package com.fuxue.service.impl;

import com.fuxue.dto.*;
import com.fuxue.entity.AnswerAll;
import com.fuxue.entity.Image;
import com.fuxue.entity.Question;
import com.fuxue.mapper.ForumMapper;
import com.fuxue.mapper.LikeMapper;
import com.fuxue.mapper.QuestionMapper;
import com.fuxue.result.PageResult;
import com.fuxue.service.ForumService;
import com.fuxue.vo.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ForumServiceImpl implements ForumService {
       @Autowired
       private ForumMapper forumMapper;
       @Autowired
       private QuestionMapper questionMapper;
       @Autowired
       private LikeMapper likeMapper;

       @Override
       public PageResult pageQuery(ForumDTO forumDTO) {
              PageHelper.startPage(forumDTO.getPage(), forumDTO.getPageSize());

              Page<ForumVO> page = forumMapper.SelectQuestion(forumDTO);
              long total = page.getTotal();
              List<ForumVO> records = page.getResult();
              return new PageResult(total, records);
       }

       @Override
       public PageResult pageAnswerQuery(ForumDTO forumDTO) {
              PageHelper.startPage(forumDTO.getPage(), forumDTO.getPageSize());
              Page<ForumAnswerVO> page = forumMapper.SelectAnswer(forumDTO);
              long total = page.getTotal();
              List<ForumAnswerVO> records = page.getResult();
              System.out.println(records);
              return new PageResult(total, records);
       }
       /*
       服务者进入论坛的问题详情
        */
       @Override
       @Transactional
       public ALLDetialsVO getALL(Integer id) {

              ALLDetialsVO allDetialsVO = new ALLDetialsVO();
              Question question = new Question();
              question = questionMapper.getQuestionALL(id);
              BeanUtils.copyProperties(question,allDetialsVO);
              //先一步到位得到相应数据
              List<String>qImage = questionMapper.getQuestionImage(id,0);
              System.out.println(question);
              int aId = likeMapper.GetAnswerId(id);
              System.out.println(aId);
              List<String>aImage = questionMapper.getQuestionImage(aId,1);

              AnswerDetialsVO answerDetialsVO = forumMapper.getAnswerDetials(aId);
              System.out.println(answerDetialsVO);
              System.out.println(allDetialsVO);
              String ss = allDetialsVO.getContext();

              BeanUtils.copyProperties(answerDetialsVO,allDetialsVO);
              allDetialsVO.setContext2(answerDetialsVO.getContext());
              allDetialsVO.setCreatTime2(answerDetialsVO.getCreatTime());
              allDetialsVO.setQImage(qImage);
              allDetialsVO.setAImage(aImage);
              allDetialsVO.setContext(ss);
              System.out.println(allDetialsVO);
              return allDetialsVO;
       }
/*
添加知识贴
 */
       @Override
       @Transactional
       public void InsertKnowledge(KnowledgeDTO knowledgeDTO) {
              knowledgeDTO.setCreatTime(LocalDateTime.now());
              forumMapper.setKnoledge(knowledgeDTO);
              Integer id = forumMapper.selectId();
              System.out.println(id+"要插入的回答id");
              List<String>list = knowledgeDTO.getUrl();
              Image image;
              for (String s:list) {
                     image = Image.builder()
                             .aqId(id)
                             .type(1)
                             .url(s)
                             .creatTime(LocalDateTime.now())
                             .build();
                     questionMapper.setImage(image);
              }
       }

       @Override
       @Transactional
       public KnowledgeVO getKnowledge(Integer id) {
              System.out.println("Id为"+id);
              AnswerAll answerAll = forumMapper.getKnowledgeVO(id);
              System.out.println("验证一下"+answerAll);
              KnowledgeVO knowledgeVO = new KnowledgeVO();
              BeanUtils.copyProperties(answerAll,knowledgeVO);
              List<String>list = forumMapper.getImage(id);
              knowledgeVO.setAImage(list);
              return knowledgeVO;
       }

       @Override
       public void newCollect(CollectionDTO collectionDTO) {
              collectionDTO.setCreateTime(LocalDateTime.now());
              forumMapper.newCollection(collectionDTO);
       }

       @Override
       public PageResult questionCollectionPageQuery(CollectionPageDTO collectionPageDTO) {
              PageHelper.startPage(collectionPageDTO.getPage(), collectionPageDTO.getPageSize());
              Page<CollectonVO> page = forumMapper.SelectQuestionCollection(collectionPageDTO);
              long total = page.getTotal();
              List<CollectonVO> records = page.getResult();
              return new PageResult(total, records);
       }

       @Override
       public PageResult AnswerCollectionPageQuery(CollectionPageDTO collectionPageDTO) {
              PageHelper.startPage(collectionPageDTO.getPage(), collectionPageDTO.getPageSize());
              Page<CollectionAnswerVO> page = forumMapper.SelectAnswerCollection(collectionPageDTO);
              long total = page.getTotal();
              List<CollectionAnswerVO> records = page.getResult();
              return new PageResult(total, records);
       }

       @Override
       public PageResult userpageQuery(ForumDTO forumDTO) {
              return null;
       }


}
